package com.guigu.wenzhiyi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.liuyunan.mapper.MealsMapper;
import com.guigu.liuyunan.mapper.SportsitemlibraryMapper;
import com.guigu.liuyunan.pojo.Meals;
import com.guigu.liuyunan.pojo.Sportsitemlibrary;
import com.guigu.wenzhiyi.mapper.FoodMapper;
import com.guigu.wenzhiyi.mapper.SchemeMapper;
import com.guigu.wenzhiyi.mapper.SprotMapper;
import com.guigu.wenzhiyi.pojo.Food;
import com.guigu.wenzhiyi.pojo.Scheme;
import com.guigu.wenzhiyi.pojo.Sprot;
import com.guigu.wenzhiyi.service.SchemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SchemeServiceImpl implements SchemeService {

    @Autowired
    SchemeMapper schemeMapper;
    @Autowired
    MealsMapper mealsMapper;
    @Autowired
    SportsitemlibraryMapper sportsitemlibraryMapper;
    @Autowired
    FoodMapper foodMapper;
    @Autowired
    SprotMapper sprotMapper;

    @Override
    public PageInfo<Scheme> page(Integer num, Integer size, String name) {
        PageHelper.startPage(num, size);
        List<Scheme> list = schemeMapper.page(name);
        for (Scheme scheme : list) {
            List<Food> foods = foodMapper.list(scheme.getId());
            List<Sprot> sprots = sprotMapper.list(scheme.getId());
            scheme.setFoods(foods);
            scheme.setSprots(sprots);
        }
        return new PageInfo<>(list);
    }

    @Override
    public List<Scheme> pagecha(String name) {
        List<Scheme> list = schemeMapper.pagecha(name);
        for (Scheme scheme : list) {
            List<Food> foods = foodMapper.list(scheme.getId());
            List<Sprot> sprots = sprotMapper.list(scheme.getId());
            scheme.setFoods(foods);
            scheme.setSprots(sprots);
        }
        return list;
    }

    @Override
    public PageInfo<Meals> MPageCha(Integer num, Integer size, String name) {
        PageHelper.startPage(num, size);
        List<Meals> list = mealsMapper.PageCha(name);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Sportsitemlibrary> SPageCha(Integer num, Integer size, String name) {
        PageHelper.startPage(num, size);
        List<Sportsitemlibrary> list = sportsitemlibraryMapper.PageCha(name);
        return new PageInfo<>(list);
    }

    @Override
    public int add(Scheme scheme) {
        int num = 0;
        System.out.println(scheme);
        if (scheme.getStarttime() != null && !scheme.getStarttime().equals("") ) {
            scheme.setStarttime(scheme.getStarttime().substring(0, 10));
        }
        if (scheme.getEndtime() != null && !scheme.getEndtime().equals("")) {
            scheme.setEndtime(scheme.getEndtime().substring(0, 10));
        }

        num += schemeMapper.insert(scheme);
        int id = scheme.getId();
        for (Food food : scheme.getFoods()) {
            food.setSchemeid(id);
            num += foodMapper.insert(food);
        }
        for (Sprot sprot : scheme.getSprots()) {
            sprot.setSchemeid(id);
            num += sprotMapper.insert(sprot);
        }
        return num;
    }

    @Override
    public int delete(Integer id) {
        int num = 0;
        num += schemeMapper.deleteByPrimaryKey(id);
        num += foodMapper.deleteByPrimaryKey(id);
        num += sprotMapper.deleteByPrimaryKey(id);
        return num;
    }

    @Override
    public int update(Scheme scheme) {
        int i = schemeMapper.updateByPrimaryKeySelective(scheme);
        foodMapper.deleteByPrimaryKey(scheme.getId());
        sprotMapper.deleteByPrimaryKey(scheme.getId());
        for (Food food : scheme.getFoods()) {
            food.setSchemeid(scheme.getId());
            i += foodMapper.insert(food);
        }
        for (Sprot sprot : scheme.getSprots()) {
            sprot.setSchemeid(scheme.getId());
            i += sprotMapper.insert(sprot);
        }
        return i;
    }
}
